<html>
<head>
<meta charset="UTF-8">
<title>资源权限列表</title>
<link id="easyuiTheme" href="../../static/easyui/themes/bootstrap/easyui.css" rel="stylesheet" type="text/css"/>
<link href="../../static/easyui/themes/icon.css" rel="stylesheet" type="text/css"/>
<script src="../../static/easyui/jquery.min.js" type="text/javascript"></script>
<script src="../../static/easyui/jquery.cookie.js" type="text/javascript"></script>
<script src="../../static/easyui/ui-theme.js" type="text/javascript"></script>
<script src="../../static/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../static/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="../../static/plugin/assets/jslib.util.js" type="text/javascript"></script>
<script src="../../static/plugin/assets/jslib.jquery.ajax.js" type="text/javascript"></script>
<script src="../../static/plugin/assets/jslib.easyui.util.js" type="text/javascript"></script>
<script src="../../ffres/dict/all.js" type="text/javascript"></script>
<script src="../../static/easyui/ui-theme.js" type="text/javascript"></script>
<a href="" target="_blank" id="commonLink" name="commonLink"></a>
	<style>
		.left {
			float: left;
			width: 100px;
		}
		.right {
			margin-left: 110px;
		}
	</style>
<script type="text/javascript">
    //页面全局变量，表示选中的角色
    var curRoleNo;
    //页面全局变量，表示选中的资源菜单
    var curResourceId;

    $(function(){
        getRolesList();
    });

	/** ************* 角色列表 start ****************** */
    //获取角色列表
    function getRolesList(){
        $.ajax({
            type: "POST",
            url: '../../api/s/roles',
            data: {
            },
            success: function(data) {
                createRolesListHtml(data.rows);
            }
        });
    }

    //创建角色列表
    function createRolesListHtml(roles) {
        var roleHtml = "<ul style='list-style-type:none'>";
        if (roles != null && roles.length > 0) {
            for (var i = 0; i < roles.length; i++) {
                roleHtml += "<li style='width:300px'>";
                roleHtml += ("<input type='radio'  name='roles' id='" + roles[i].roleno + "' onclick='clickRole(this)'>" + roles[i].rolename);
            }
        }
        roleHtml += "</ul>";
        document.getElementById("rolesList").innerHTML = roleHtml.toString();
    }

    //角色点击事件
    function clickRole(roleObj){
        curRoleNo = roleObj.id;
        //判断当前是否有选择资源菜单，有则查询资源权限信息
		if(null != curResourceId && '' != curResourceId){
            getResActs(curRoleNo, curResourceId);
		}
    }
    /** ************* 角色列表 end ****************** */


    /** ************* 系统菜单 start ****************************************** */
    //菜单资源树形列表点击事件
    function clickResNode(node) {
        curResourceId = node.id;
        queryResActs(curResourceId);
        //判断当前是否有选择角色，有则查询资源权限信息
        if(null != curRoleNo && '' != curRoleNo){
            setTimeout(function(){
                getResActs(curRoleNo, curResourceId);
            }, 200);
		}
    }
    /** ************* 系统菜单 end ************************************************** */

    /** ************* 资源操作多选框 start *****************/
    //查询当前菜单资源的资源操作信息
    function queryResActs(resourceId){
        $.ajax({
            type: "POST",
            url: '../../api/s/resource/action/all',
            data: {
                resourceid: resourceId
            },
            success: function(data) {
                createSRecActListHtml(data.rows);
            }
        });
    }

    //获取当前角色、菜单资源的资源操作信息
    function getResActs(roleNo, resourceId){
        $.ajax({
            type: "POST",
            url: '../../api/s/role/rights/all',
            data: {
                roleno: roleNo,
                resourceid: resourceId
            },
            success: function(data) {
                setSRecAct(data.rows);
            }
        });
    }

    //创建资源操作多选框
    function createSRecActListHtml(resActs) {
        var sRecActHtml = "";
        if (resActs != null && resActs.length > 0) {
            for (var i = 0; i < resActs.length; i++) {
                // sRecActHtml += "<div style='width:100px'>";
                sRecActHtml += ("<input type='checkbox' name='actId' value='" + resActs[i].actid + "'>" + resActs[i].descr);
                sRecActHtml += "<br />";
            }
        }
        document.getElementById("sRecActList").innerHTML = sRecActHtml.toString();
    }

    //设置已配置的角色资源操作信息
    function setSRecAct(sRecActs) {
        var actIds = document.getElementsByName('actId');
        for (var i = 0; i < actIds.length; i++) {
            actIds[i].checked = false;
        }
        for (var i = 0; i < actIds.length; i++) {
            for (var j = 0; j < sRecActs.length; j++) {
                if (sRecActs[j].actid == actIds[i].value) {
                    actIds[i].checked = true;
                }
            }
        }
    }

    //角色菜单资源操作信息保存
    function doSaveRoleResActs(){
        if(null == curRoleNo || '' == curRoleNo){
            $.messager.alert('提示','请选择角色！','info');
            return;
		}
        if(null == curResourceId || '' == curResourceId){
            $.messager.alert('提示','请选择菜单资源！','info');
            return;
        }
        var actIds = "";
        $("input[name='actId']:checked").each(function(i){
            actIds = actIds + $(this).val() + ",";
        });
		if(null != actIds && "" != actIds)
		    actIds = actIds.substr(0, actIds.length-1);
        var jsonData = JSON.parse('{'+'"roleno":"' + curRoleNo + '"'
			+ ','+'"resourceid":"' + curResourceId + '"'
			+ ','+'"actIdStr":"' + actIds + '"'+'}');

        $.restfulPostJson('../../api/s/role/right/save', jsonData, function(data) {
			if(data.success){
                $.messager.alert('提示','保存成功！','info');
			}else{
                $.messager.alert('错误','保存失败！','error');
			}
        });
	}
    /** ************* 资源操作多选框 end *****************/

    // function getSSubsInfo() {
    //     $.ajax({
    //         type: "POST",
    //         url: '../../api/s/subs/infos',
    //         data: {
    //         },
    //         success: function(data) {
    //             if (data!=null){
    //                 if (data!=null){
    //                     $('#cc').combobox({
    //                         data: data.rows,
    //                         valueField: 'subsCode',
    //                         textField: 'subsName'}
    //                     );
    //                 }
    //             }
    //         }
    //     });
    // }
	//
    // // 下拉框选择改变事件，生成菜单树
    // $(function () {
    //     $("#cc").combobox({
    //         //相当于html >> select >> onChange事件
    //         onChange:function(subsCode){
    //             getTreeBySystempk(subsCode)
    //         }
    //     });
    // })
	//
    // //根据Systempk(subs_code) 获取树菜单
    // function getTreeBySystempk(name) {
    //     $("#tt").tree({
    //         url:'../../api/s/resource//all',// 请求路径
    //         queryParams: {
    //             systempk: name,
    //         },
    //         onLoadSuccess:function(node,data){// 成功加载树之后的操作
    //             var tree = $('#tt');
    //             if(data){
    //                 $(data).each(function(index,d) {// 遍历生成节点
    //                     if (this.state=='closed') {
    //                         tree.tree('expandAll');
    //                     }
    //                 });
    //             }
    //         }
    //     });

    // }


</script>
</head>
<body class="easyui-layout">

	<!-- 左侧 角色列表 -->
	<div data-options="region:'west',split:true" style="width: 400px">
		<div class="easyui-panel" style="width:100%; background: #eee;" fit="true" title="角色列表" border="false">
			<div id="rolesList" style="width:350px">
			</div>
		</div>
	</div>

	<!-- 中间  内容主体 -->
	<div data-options="region:'center',border:false" title="系统菜单" style="padding: 0px; background: #fafafa;">
		<ul id="tt" class="easyui-tree" url="../../api/s/resource/all/root" data-options="animate:true, onClick:clickResNode">
			<!-- 动态加载数据 -->
		</ul>
	</div>

	<!-- 右侧 资源操作 -->
	<div data-options="region:'east',split:true" style="width: 300px;">
		<div class="easyui-panel" style="width:100%;" fit="true" title="资源操作" border="false">
			<div id="sRecActList" style="width:250px; padding: 10px">
			</div>
			<div style="padding:20px; margin-bottom:5px; text-align: center">
				<a href="javascript:doSaveRoleResActs()" class="easyui-linkbutton">保存</a>
			</div>
		</div>
	</div>

</body>
</html>
